<template>
    <div id="StudentNumberChart" :style="{ width: props.width, height: props.height }"></div>
</template>
<script setup lang="ts">
import { GetStudentNumber } from '@/controllers/adminController';
import { onMounted } from 'vue';
import * as echarts from 'echarts';
const props = defineProps<{ width: string, height: string }>();

onMounted(() => {
    try {
        drawChart();
    } catch (error) {
        console.error(error);
    }
})
async function drawChart() {
    const studentData = await GetStudentNumber();
    const c = document.getElementById('StudentNumberChart')
    if (!c) {
        console.error('元素不存在！')
        return;
    }
    const chart = echarts.init(c);
    var option: echarts.EChartsOption = {
        title: {
            text: '各班级学生人数',
            subtext: '全年级',
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [
            {
                name: '班级人数',
                type: 'pie',
                radius: '50%',
                data: studentData,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    }
    chart.setOption(option)
}
</script>